<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="100px">
    <el-form-item :label="$t('nick_name')" prop="nickName">
      <el-input v-model="user.nickName" />
    </el-form-item>
    <el-form-item :label="$t('mobile_phone_number')" prop="phonenumber">
      <el-input v-model="user.phonenumber" />
    </el-form-item>
    <el-form-item :label="$t('emailbox')" prop="email">
      <el-input v-model="user.email" />
    </el-form-item>
    <el-form-item :label="$t('gender')">
      <el-radio-group v-model="user.sex">
        <el-radio label="0">{{ $t("male") }}</el-radio>
        <el-radio label="1">{{ $t("female") }}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">{{
        $t("save")
      }}</el-button>
      <el-button type="danger" size="mini" @click="close">{{
        $t("cancel")
      }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserProfile } from "@/api/system/user";

export default {
  props: {
    user: {
      type: Object,
    },
  },
  data() {
    return {
      // 表单校验
      rules: {
        nickName: [
          {
            required: true,
            message: this.$t("cannot_empty", { name: this.$t("nick_name") }),
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: this.$t("cannot_empty", { name: this.$t("emailbox") }),
            trigger: "blur",
          },
          {
            type: "email",
            message: this.$t("correct_format"),
            trigger: ["blur", "change"],
          },
        ],
        phonenumber: [
          {
            required: true,
            message: this.$t("cannot_empty", {
              name: this.$t("mobile_phone_number"),
            }),
            trigger: "blur",
          },
          {
            pattern:  /^((\+|00)86)?1[3-9]\d{9}$|^852\d{8}$|^853\d{8}$|^(886|0)9\d{8}$/,
            
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
      },
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateUserProfile(this.user).then((response) => {
            this.$modal.msgSuccess(this.$t("operation_successful"));
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    },
  },
};
</script>
